#include "MockTables.h"
#include "MockTable.h"
#include "memdb/error/MemdbError.h"

namespace memdb {
namespace impl {
namespace mock {

MockTables::MockTables() 
{
}
MockTables::~MockTables() {
  for(auto & p : _tables){
    delete p.second;
  }
}

std::pair<std::error_code, MockTables::ValueType> MockTables::create(const KeyType & name) {
  auto && iter = _tables.find(name);
  if(iter == _tables.end()){
    auto * tab = new MockTable();
    _tables.insert(std::make_pair(name, tab));
    return std::make_pair(memdb::error::ok, tab);
  }
  return std::make_pair(memdb::error::table_exists, nullptr);
}

std::error_code MockTables::drop(const KeyType & name) {
  auto && iter = _tables.find(name);
  if(iter != _tables.end()){
    delete iter->second;
    _tables.erase(iter);
    return memdb::error::ok;
  }
  return memdb::error::table_not_exists;
}

bool MockTables::exists(const KeyType & name) const {
  return _tables.find(name) != _tables.end();
}

std::optional<MockTables::ValueType> MockTables::get(const KeyType & name) const {
  auto && iter = _tables.find(name);
  if(iter != _tables.end()){
    return std::make_optional<ValueType>(iter->second);
  }
  return std::nullopt;
}

size_t MockTables::count() const {
  return _tables.size();
}

std::unique_ptr<MockTables::Iterator> MockTables::iterator() const {
  return nullptr;
}

}//namespace mock
}//namespace impl
}//namespace memdb
